import 'package:dmz/base/controller/base_controller.dart';
import 'package:dmz/business/home/controller/home_main_controller.dart';
import 'package:dmz/business/home/model/locus_data.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/business/home/model/run_message.dart';
import 'package:dmz/business/home/widgets/run_message_widgets.dart';
import 'package:dmz/request/apis.dart';
import 'package:dmz/request/request_client.dart';
import 'package:dmz/routers/routers.dart';
import 'package:dmz/tools/map/map_manger.dart';
import 'package:dmz/utils/customize_loding.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class RunMessageController extends BaseController {
  late MotoModel data;
  int pageIndex = 0;
  RunMessageModel? pageModel;
  PageController pagect = PageController();
  late String dateStr;
  late LocusPageData locusPageData;
  final ScrollController scrollController = ScrollController();
  @override
  void loadNet() {}

  @override
  void onHidden() {}

  @override
  void onInit() {
    dateStr = SPUtils.getTimeStr();
    var controllered = Get.find<HomePageController>();
    data = controllered.motoList[controllered.pageIndex];
    getPgaeData();
    super.onInit();
    getRouteListData();
  }

  ///骑行统计页面数据
  getPgaeData() {
    ///骑行数据总数据
    RequestClient().post<Map<String, dynamic>>(APIS.runingData,
        data: {'ebike_id': data.id}).then((value) async {
      pageModel ??= RunMessageModel();
      pageModel?.accompany_days = (value?['accompany_days'] ?? 0);
      pageModel?.max_ride_mileage =
          (value?['max_ride_mileage'] ?? 0) / 1000 * 1.0;
      pageModel?.total_mileage = (value?['total_mileage'] ?? 0) / 1000 * 1.0;
      update(['count']);
    });

    ///骑行数据15天数据
    RequestClient().post<Map<String, dynamic>>(APIS.trackStatics, data: {
      'ebike_id': data.id,
      'time_start': SPUtils.getSubtractDay(15),
      'end_time': SPUtils.getSubtractDay(0)
    }).then((value) async {
      pageModel ??= RunMessageModel();
      pageModel?.accumulated_mileage =
          (value?['accumulated_mileage'] ?? 0) / 1000 * 1.0;
      pageModel?.max_speed = (value?['max_speed'] ?? 0) * 1.0;
      pageModel?.avg_speed = (value?['avg_speed'] ?? 0) * 1.0;
      pageModel?.cycling_count = (value?['cycling_count'] ?? 0);
      pageModel?.mileage_statics = (value?['mileage_statics'] as List<dynamic>?)
          ?.map((e) => e as Map<String, dynamic>)
          .toList();
      pageModel?.getMaxdata();
      update(['count']);
      Future.delayed(const Duration(seconds: 1), () {
        scrollController.jumpTo(scrollController.position.maxScrollExtent);
      });
    });
  }

  ///轨迹列表
  getRouteListData() {
    RequestClient().post<LocusPageData>(APIS.routeList, data: {
      'ebike_id': data.id,
      'time_start': SPUtils.getTimeInt(dateStr),
      'end_time': SPUtils.getTimeInt('$dateStr 23:59:59')
    }).then((value) async {
      // debugPrint('轨迹列表 ----- value');
      if (value != null) {
        locusPageData = value;
        update(['Locus']);
      }
    });
  }

  ///日期点击
  topClick(int select) {
    pageIndex = select;
    pagect.jumpToPage(pageIndex);
    update();
  }

  ///日期选择
  selectDate() {
    EMILoding.showBottomWindow(
        RunMessageWidgets.seleDateWheelWidget(dateStr, (value) {
          ///时间选择完成
          debugPrint('时间选择完成 --- $value');
          Get.back();
          dateStr = value;
          getRouteListData();
          update(['Locus']);
        }),
        isDismissible: true,
        showBack: true);
  }

  ///列表点击
  listClickLocus(LocusModle data) async {
    var datas = await EMIMapManger().convertMoreLatLng(data.track_list!);
    var result =
        await EMIMapManger().trajectoryCorrection(data.track_list!, datas);
    if (result == null) {
      return EMILoding.hitLoding('轨迹数据错误'.tr);
    }
    Get.toNamed(RouteGet.mapLocus, arguments: {'result': result, 'data': data});
  }
}

class RunMessageBinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => RunMessageController());
  }
}
